package com.pai.gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.pai.entity.MyInfo;

//import TestLoginGui.LoginBackground;
/**
 * 登陆框Frame
 * @author Vido
 *
 */
public class LoginGUI extends TranslucentWindow {
	/**
	 *登陆的构造方法
	 */
	public LoginGUI(){
		this.setBounds(500, 200, 400, 380);
		/**
		 * 设计JPanel画背景
		 */
		JPanel pane = new JPanel() { 
	        @Override 
	        public void paintComponent(Graphics g) { 
	        	super.paintComponent(g);
	            g.drawImage(new ImageIcon("res/icon/LoginBackgroound1.png").getImage(), 0, 0, 400, 380, this);
	            
	        } 
	    };
		pane.setLayout(null);//自定义布局
		pane.setOpaque(false);//设置背景透明
	
		
		//关闭按钮
		Icon exitIcon = new ImageIcon("res/icon/LoginExitButton.png");
		JButton Exit = new JButton(exitIcon);
		Exit.setBounds(349, 24, 25, 23);
		Exit.setBorder(BorderFactory.createEmptyBorder());
		
		//注册关闭按钮的动作监听器
		Exit.addActionListener(new ActionListener() {	
			@Override
			public void actionPerformed(ActionEvent e) {
					System.exit(0);//关闭系统
			}
		});
		//设置鼠标经过和点击关闭按钮时的按钮样式
		Exit.setRolloverIcon(new ImageIcon("res/icon/LoginExitButton2.png"));
		Exit.setPressedIcon(new ImageIcon("res/icon/LoginExitButton3.png"));

		//设置登陆界面的头像框背景和头像设置
		JPanel Photo = new JPanel() { 
	        @Override 
	        public void paint(Graphics g) { 
	            super.paint(g); 
	            g.drawImage(new ImageIcon("res/icon/PhotoFrame.png").getImage(), 0, 0, 143, 143, this);
	        } 
	    };
		Photo.setBounds(127, 47, 143, 143);
		Photo.setOpaque(false);

		//设置头像选择按钮（当前版本中未添加）
		JPanel ButtonPanel = new JPanel(); 
		ButtonPanel.setBounds(250, 177, 28, 14);
		ButtonPanel.setOpaque(false);
		ButtonPanel.setBorder(BorderFactory.createEmptyBorder());
		ButtonPanel.setLayout(null);//自定义布局
		Icon choosePhoto = new ImageIcon("res/icon/ChoosePhotoButton.png");
		JButton ChoosePhoto = new JButton(choosePhoto);
		ChoosePhoto.setBounds(0, 0, 24, 14);
		ChoosePhoto.setBorder(BorderFactory.createEmptyBorder());
		
		ChoosePhoto.addActionListener(new ActionListener() {	
			@Override
			public void actionPerformed(ActionEvent e) {
					System.exit(0);//set other actions
			}
		});
		
		ButtonPanel.add(ChoosePhoto);

			
			
		//设置放置输入框的JPanel
		JPanel Input = new JPanel() { 
	        @Override 
	        public void paint(Graphics g) { 
	            super.paint(g); 
	            g.drawImage(new ImageIcon("res/icon/Input.png").getImage(), 0, 0, 254, 35, this);
	        } 
	    };
		Input.setBounds(75, 238, 254, 35);
		Input.setOpaque(false);
		Input.setLayout(null);//自定义布局
		
		//设置输入框
		final JTextField input = new JTextField();
		
		input.setBorder(BorderFactory.createEmptyBorder());
		input.setBackground(new Color(10,10,10));
		input.setBounds(73, 7, 174, 23);
		//设置字体
		input.setFont(new Font(/*"Adobe 黑体 Std"*/"Arial",Font.PLAIN,18));
		input.setForeground(Color.white);
		input.enableInputMethods(false);
		Input.add(input);

		
		
		
		//登陆按钮
		Icon loginIcon = new ImageIcon("res/icon/LoginButton.png");
		JButton Login = new JButton(loginIcon);
		Login.setBounds(116, 286, 173, 32);
		Login.setBorder(BorderFactory.createEmptyBorder());
		
		Login.addActionListener(new ActionListener() {	
			@Override
			public void actionPerformed(ActionEvent e) {
					String username = input.getText();
					MyInfo.me.setUsername(username);
					MyInfo.me.setUserHeadNum(1);
					//显示主界面，隐藏登陆界面
					new MainFrame();
					setVisible(false);
					//store user info.
			}
		});
		Login.setRolloverIcon(new ImageIcon("res/icon/LoginButton2.png"));
		Login.setPressedIcon(new ImageIcon("res/icon/LoginButton3.png"));
		
		
		pane.add(Exit);
		pane.add(Photo);
		
		pane.add(Login);
		pane.add(Input);
		input.requestFocus();
		//设置回车按键确认登陆
		this.getRootPane().setDefaultButton(Login);
	    this.setContentPane(pane); 
	    this.setVisible(true);
	    //设置输入框的自动获取焦点
	    input.requestFocus();
}
	}
